package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"user-app/models"
)

type MemberController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *MemberController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	query := models.DB.Model(&models.Member{})
	var count int64
	err := query.Count(&count).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "总数据查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	offset := (page - 1) * size
	member := []models.Member{}
	err = query.Offset(offset).Limit(size).Find(&member).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
	}
	pageData := make(map[string]interface{})
	pageData["count"] = count
	pageData["member"] = member
	c.Data["json"] = JsonR{
		Code: 200,
		Msg:  "查询成功",
		Data: member,
	}
	c.ServeJSON()
}
